<template>
  <div>
    <!-- <item v-for="(value,index) in initGood" :key="index" :itemInfo="value">
    </item> -->
    <item v-for="(value,index) in getGoods" @deleteGood="deleteGood" :key="index" :itemInfo="value">
      <button slot="button" class="button">删 除</button>
    </item>
  </div>
</template>

<script>
  import item from './goodsListItem.vue'
  import {carDelData,carGetData} from 'network/car/emit.js'
  import {mapGetters} from 'vuex'
  export default{
    name:'',
    data(){
      return {
        initGood:[],
        test:1,
        price:0
       }
    },
    computed:{
      ...mapGetters(['getGoods'])

    },
    methods:{
      sendGoodToBuy(){
        let sendGood=[]
        for(let i of this.getGoods){
          console.log(i);
          if(i.pay){
            i.num=i.count
            i.goodId=i.id
            i.goodName=i.name
            sendGood.push(i)
          }
        }
        if(sendGood.length==0){
          return 0
        }
        console.log('send',sendGood);
        this.$router.push("/buy")
        window.sessionStorage.setItem("buyGood",JSON.stringify(sendGood))
        // this.$bus.$emit("sendGoodToBuy",sendGood)
        }
    },
    activated() {
      let id=JSON.parse(window.sessionStorage.user).id
      carGetData(id).then((res)=>{
        this.initGood=res.data.list
        console.log(this.test);
        for(let i of this.initGood){
          i.count=i.num
          i.pay=true
          i.desc=i.discount
          i.id=i.goodId
          delete i.goodId
          delete i.num
          delete i.discount
          this.$store.dispatch('addGoods',i).then(res=>console.log(res))
        }
      }).catch(e=>{
        console.log(e);
      })
    },
    mounted() {
      this.$bus.$on("carBuy",()=>{
        this.sendGoodToBuy()
      })
    },
    components:{
      item
    }
  }
</script>

<style scoped>

</style>
